﻿namespace CShorpPro._1009;
public class Demo
{
    public void TestPrint()
    {
        int num = 10;
        while (num > 5)
        {
            Console.Write(num+"\t");
            num--;
        }
    }

    public void Testprint1()
    {
        int num = 10;
        do
        {
            
            num--;
        } while (num > 5);
    }

    public void TestPrint2()
    {
        int num = 10;
        for (int i = 0; i < 10; i++)
        {
            if (i % 2 != 0)
            {
                Console.Write(num + "\t");
            }
        }

    }
    public static void TestPrint3()
    {
        Console.WriteLine("1000以内的水仙花数：");
    
        // 遍历1到999的所有数字
        for (int number = 1; number < 1000; number++)
        {
            int temp = number;
            int digitCount = 0;
            int sum = 0;
        
            // 计算数字的位数
            int tempForCount = number;
            while (tempForCount > 0)
            {
                digitCount++;
                tempForCount /= 10;
            }
        
            // 计算各位数字的digitCount次方之和
            temp = number;
            while (temp > 0)
            {
                int digit = temp % 10;
                int power = 1;
            
                // 使用循环计算digit的digitCount次方
                for (int i = 0; i < digitCount; i++)
                {
                    power *= digit;
                }
            
                sum += power;
                temp /= 10;
            }
        
            // 判断是否为水仙花数
            if (sum == number)
            {
                Console.Write(number + "\t");
            }
        }
        Console.WriteLine();
        
    }

    public static void TestPrint4()
    {
        Console.WriteLine("1000以内的质数：");
    
        // 从2开始遍历到1000
        for (int number = 2; number < 1000; number++)
        {
            bool isPrime = true;
            int divisor = 2;
        
            // 使用循环检查是否为质数
            while (divisor * divisor <= number)
            {
                if (number % divisor == 0)
                {
                    isPrime = false;
                    break;
                }
                divisor++;
            }
        
            // 如果是质数则输出
            if (isPrime)
            {
                Console.Write(number + "\t");
            }
        }
        Console.WriteLine();
    }

}